package xyz.zdsoft.common_module.service.Impl;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import xyz.zdsoft.common.utils.JwtUtils;
import xyz.zdsoft.common.utils.Result;
import xyz.zdsoft.common_module.dto.ChangePwd;
import xyz.zdsoft.common_module.mapper.CommonMapper;
import xyz.zdsoft.common_module.mapper.result.*;
import xyz.zdsoft.common_module.service.CommonService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Service
public class CommonServiceImpl implements CommonService {

    private final CommonMapper commonMapper;

    public CommonServiceImpl(CommonMapper commonMapper) {
        this.commonMapper = commonMapper;
    }

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public Result getCourse(HttpServletRequest request) {
        String e = JwtUtils.getMemberUserTypeByJwtToken(request);
        List<Course> list = (JwtUtils.getMemberUserTypeByJwtToken(request).equals("student"))?commonMapper.getCourse(JwtUtils.getMemberIdByJwtToken(request)):commonMapper.getCourse("-1");
        if(list==null||list.size()==0) return Result.error().message("课程为空");
        return Result.ok().data(list);
    }

    @Override
    public Result getTeacher(String teacherName) {
        List<Teacher> list = commonMapper.getTeacher(teacherName);
        if(list==null||list.size()==0) return Result.error().message("老师为空");
        return Result.ok().data(list);

    }

    @Override
    public Result getTeam() {
        List<Team> list = commonMapper.getTeam();
        if(list==null||list.size()==0) return Result.error().message("小组为空");
        return Result.ok().data(list);
    }

    @Override
    public Result getCollege() {
        List<College> list = commonMapper.getCollege();
        if(list==null||list.size()==0) return Result.error().message("院系为空");
        return Result.ok().data(list);
    }

    @Override
    public Result getMajor(Integer collegeId) {
        List<Major> list = (collegeId==null || collegeId==0)?commonMapper.getMajor(0):commonMapper.getMajor(collegeId);
        if(list==null||list.size()==0) return Result.error().message("专业为空");
        return Result.ok().data(list);
    }

    @Override
    public Result getGrade() {
        List<Grade> list = commonMapper.getGrade();
        if(list==null||list.size()==0) return Result.error().message("年级为空");
        return Result.ok().data(list);
    }

    @Override
    public Result getClassInfo() {
        List<ClassInfo> list = commonMapper.getClassInfo();
        if(list==null||list.size()==0) return Result.error().message("班级为空");
        return Result.ok().data(list);
    }

    @Override
    public Result updatePassword(HttpServletRequest request, ChangePwd cp) {
        Pass pass = commonMapper.selectPassById(Integer.parseInt(JwtUtils.getMemberIdByJwtToken(request)));
        if(pass.getIsFirstLogin()==0) {
            if(cp.getOldPwd()==null||cp.getOldPwd()=="") return Result.error().message("请输入旧密码");
            if(!cp.getOldPwd().equals(pass.getPassword())) return Result.error().message("旧密码输入错误");
        }
        if(cp.getNewPwd()==null||cp.getNewPwd()=="") return Result.error().message("请输入新密码");
        if(!cp.getNewPwd().matches("^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)])+$).{6,16}$"))
            return Result.error().message("密码至少包含：数字,英文,字符中的两种以上，长度6-16");
        if(cp.getConPwd()==null||cp.getConPwd()=="") return Result.error().message("确认密码不能为空");
        if(!cp.getNewPwd().equals(cp.getConPwd())) return Result.error().message("新密码和确认密码不一致");
        if(pass.getPassword().equals(cp.getNewPwd())) return Result.error().message("新密码不能和旧密码一样");
        int result = commonMapper.updatePassword(cp.getNewPwd(),Integer.parseInt(JwtUtils.getMemberIdByJwtToken(request)));
        if(result==1){
            redisTemplate.delete(JwtUtils.getToken(request));
            return Result.ok().message("修改密码成功！请重新登录");
        }
        return Result.error().message("修改密码失败，请重试");
    }
}
